package web;

import Utils.JDBCUtils;
import beans.RegisterStatus;
import beans.User;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import service.accountService;
import Utils.JSONUtils;

import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(value = "/user/account")
public class queryAccountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        response.setHeader("Access-Control-Allow-Origin", "前端域名，如 http://localhost:5173");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
//        String username = request.getParameter("username");
        BufferedReader reader = request.getReader();
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        User queryUser = JSONUtil.toBean(stringBuilder.toString(), User.class);
        User user = new accountService().queryAccount(queryUser.getUsername());
        if (user != null) {
            response.getWriter().write(JSONUtils.result(RegisterStatus.USER_EXIST, user));
        }else {
            response.getWriter().write(JSONUtils.result(RegisterStatus.USER_NOT_EXIST,user));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}





